<?php

namespace App\Http\Middleware;

use App\Models\Traits\PublicId;
use \Illuminate\Http\Request;
use Closure;

/**
 * Class Decode
 * @package App\Http\Middleware
 */
class Idecode
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     *
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $params = $request->all();
        return $next($request);

        foreach ($params as $k => $v) {
            //防止xss攻击
            //$v = string_remove_xss($v);

            if (!preg_match('/ids?$/i', $k)) {
                continue;
            }

            if (is_array($v)) {
                foreach ($v as $key => $value) {
                    $v[$key] = true_id($value);
                }
                $decodeRs = $v;
            } elseif (strpos($v, ',') === false) {
                $decodeRs = true_id($v);
            } else {
                $ids = explode(',', $v);
                $decodeIds = [];
                foreach ($ids as $idString) {
                    $decodeIds[] = true_id($idString);
                }
                $decodeRs = implode(',', $decodeIds);
            }

            $params[$k] = $decodeRs ?? $v;
        }

        $request->replace($params);
        return $next($request);
    }
}
